//
//  TaskCalendarItemView.m
//  Task Views
//
//  Created by Chris Karr on 7/13/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "TaskCalendarItemView.h"
#import "TaskCalendarView.h"

@implementation TaskCalendarItemView

@synthesize task;
@synthesize highlighted;
@synthesize lastDown;
@synthesize backgroundColor;

- (id)initWithFrame:(NSRect)frame 
{
    self = [super initWithFrame:frame];

    if (self) 
	{
		self.highlighted = [NSNumber numberWithBool:NO];
		self.lastDown = [NSDate date];
		self.backgroundColor = [NSColor clearColor];
    }

    return self;
}

- (void) drawBackground:(NSRect) rect
{
	[self.backgroundColor setFill];
	[self.backgroundColor setStroke];
	
	NSBezierPath * path = [NSBezierPath bezierPathWithRect:rect];
	
	[path fill];
	[path stroke];
}

- (void) drawRect:(NSRect) rect 
{
	NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
	
	rect = [self bounds];
	
	[self drawBackground:rect];

	NSRect newRect = NSMakeRect(rect.origin.x + 1, rect.origin.y + 1, rect.size.width - 2, rect.size.height - 2);
	
	NSBezierPath * path = [NSBezierPath bezierPathWithOvalInRect:newRect];

	if ([self.highlighted boolValue])
	{
		NSColor * selectColor = [NSColor whiteColor];
		
		NSData * color = [defaults valueForKey:@"cal_selection_color"];
		if (color != nil)
			selectColor = [NSKeyedUnarchiver unarchiveObjectWithData:color];
		
		[selectColor setFill];
	}
	else
	{
		NSData * color = [defaults valueForKey:@"cal_default_color"];
		
		NSColor * defaultColor = [NSColor blackColor];
		if (color != nil)
			defaultColor = [NSKeyedUnarchiver unarchiveObjectWithData:color];
		
		[defaultColor setFill];
	}
	
	[path fill];
}

- (BOOL) acceptsFirstResponder
{
	return YES;
}

- (void)mouseDown:(NSEvent *)theEvent
{
	[((TaskCalendarView *) [self superview]) setSelectedItem:self.task];
	
	if ([self.lastDown timeIntervalSinceNow] > -1)
		[((TaskCalendarView *) [self superview]) editTask];
	
	self.lastDown = [NSDate date];
}

- (void) viewDidMoveToWindow 
{
	tag = [self addTrackingRect:[self bounds] owner:self userData:nil assumeInside:NO];
}


@end
